<%@page import="study.itwill.team5.board.Board"%>
<%@page import="study.itwill.team5.board.BoardItem"%>
<%@page import="study.itwill.jsp.PageHandler"%>
<%@page import="study.itwill.jsp.MultipartBox.FileInfo"%>
<%@page import="study.itwill.common.DateTime"%>
<%@page import="study.itwill.team5.admin.AdminItem"%>
<%@page import="study.itwill.jsp.WebHandler"%>
<%@page import="study.itwill.team5.house.RoomItem"%>
<%@page import="study.itwill.team5.house.TelItem"%>
<%@page import="study.itwill.team5.house.HouseItem"%>
<%@page import="study.itwill.team5.house.House"%>
<%@page import="study.itwill.common.DBHandler"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ include file="../session.jsp"%>
<%@ include file="../dbcon.jsp"%>
<%
	DateTime datetime = DateTime.getInstance();

	int seq = web.getInt("seq");
	
	if (seq == -1) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}
%>
<%
	//게시판 카테고리에 대한 ArrayList 받아오기
	BoardCategory category = new BoardCategory();
	ArrayList<BoardCategoryItem> category_list = category.listBoardCategory();
	
	int category_seq = -1;
	for (int i=0; i<category_list.size(); i++) {
		BoardCategoryItem item = category_list.get(i);
		if (item.getCategory_name().indexOf("후기") > 0) {
			category_seq = item.getSeq();
		}
	}

	//페이지 번호 파라미터
	int nowPage = web.getInt("page");
	
	// 파라미터가 없다면 -1을 리턴하므로...
	if (nowPage < 1) {
		nowPage = 1;
	}
	
	// 전체 데이터 수 조회
	int total_count = db.getCount("board", "seq", "board_category_seq=" + category_seq);
	
	// 페이징 객체 호출
	PageHandler p_handler = PageHandler.getInstance(nowPage, total_count, 10, 10);
	
	// 목록을 가져오는 메서드에게 데이터의 시작위치와 끝 위치 전달
	int start = p_handler.getStartRec();
	int end = p_handler.getPageArticleCount();

	ArrayList<BoardItem> board_list = (new Board()).listBoard(2, "" + seq, start, end);

	if (board_list == null) {
		db.disconnect();
		web.goPage(null, "데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.");
		return;
	}
	
	/** 민박데이터 조회 */
	House house = new House();
	HouseItem item = house.viewHouse(seq);
	ArrayList<TelItem> tel_list = house.viewTel(seq);
	ArrayList<RoomItem> room_list = house.viewRoom(seq);
	
	if (item == null) {
		db.disconnect();
		web.goPage(null, "데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.");
		return;
	}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<%@ include file="../head.jsp"%>
<style>
.link {
	color: #333 !important;
}
</style>
</head>
<body>
	<%@ include file="../menu.jsp"%>
	<div class="container">
		<div class="page-header">
			<h1>민박 정보 보기</h1>
		</div>

		<form id="frmAdminView" class="form-horizontal" role="form"
			method="post" action="">
			<!-- 민박과 도시 일련번호 자동입력 -->
			<input type="hidden" name="house_seq" value="<%=item.getSeq()%>">
			<input type="hidden" name="city_seq"
				value="<%=item.getHouse_city_seq()%>">

			<fieldset>

				<div class="form-group">
					<label for="house_city_seq" class="col-md-2 control-label">민박
						도시</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getCity_name()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="name" class="col-md-2 control-label">민박이름</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getName()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="address1" class="col-md-2 control-label">나라</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getAddress1()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="address2" class="col-md-2 control-label">도시</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getAddress2()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="address3" class="col-md-2 control-label">상세주소</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getAddress3()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="address4" class="col-md-2 control-label">간단주소</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getAddress4()%></p>
					</div>
				</div>
				
				<div class="form-group">
					<label for="lat" class="col-md-2 control-label">위도</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getLat()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="lon" class="col-md-2 control-label">경도</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getLon()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="email" class="col-md-2 control-label">이메일</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getEmail()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="homepage" class="col-md-2 control-label">홈페이지</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getHomepage()%></p>
					</div>
				</div>
				
				<div class="form-group">
					<label for="lon" class="col-md-2 control-label">평점</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getGrade()%>점 (<%=item.getGrade_count()%>건)</p>
					</div>
				</div>

				<div class="form-group">
					<label for="info" class="col-md-2 control-label">상세정보</label>
					<div class="col-md-10">
						<p class="form-control-static"><%=item.getInfo()%></p>
					</div>
				</div>

				<div class="form-group">
					<label for="reg_date" class="col-md-2 control-label">등록일자</label>
					<div class="col-md-10">
						<p class="form-control-static"><%= datetime.getDateTimeString(item.getReg_date()) %></p>
					</div>
				</div>

				<div class="form-group">
					<label for="edit_date" class="col-md-2 control-label">수정일자</label>
					<div class="col-md-10">
						<p class="form-control-static"><%= datetime.getDateTimeString(item.getEdit_date()) %></p>
					</div>
				</div>

				<div class="form-group">
					<label for="tel[]" class="col-md-2 control-label">연락처</label>
					<div class="col-md-10">
						<%
	if (tel_list == null) {
%>
						<p class="form-control-static">연락처 정보가 없습니다.</p>
						<%
	} else {
		for (int i=0; i<tel_list.size(); i++) {
			TelItem tel_item = tel_list.get(i);
%>
						<p class="form-control-static"><%=tel_item.getTel()%></p>
						<%		
		}
	}
%>
					</div>
				</div>

				<div class="form-group">
					<label for="image[]" class="col-md-2 control-label">이미지</label>
					<div class="col-md-10">
<%
	ArrayList<FileInfo> imageList = item.getImageList();
	if (imageList != null) {
		for (int i=0; i<imageList.size(); i++) {
			FileInfo info = imageList.get(i);
			String src = "/Team-Project/download.jsp?" + info.toString("utf-8");
%>
							<p class="form-control-static">
								<a href="<%= src %>" data-lightbox="roadtrip">
									<img src="<%= src %>" class="img-thumbnail img-responsive" width="300"/>
								</a>
							</p>
<%			
		}
	}
%>
					</div>
				</div>

				<div class="form-group">
					<label for="room[]" class="col-md-2 control-label">룸</label>
					<div class="col-md-10">
<%
	if (room_list == null) {
%>
						<p class="form-control-static">룸 정보가 없습니다.</p>
<%
	} else {
%>
						<div class="table-responsive">
							<table class="table table-condensed">
								<thead>
									<tr class="warning row">
										<th class="text-center col-md-3">룸이름</th>
										<th class="text-center col-md-3">최대인원</th>
										<th class="text-center col-md-3">비수기가격</th>
										<th class="text-center col-md-3">성수기가격</th>
									</tr>
								</thead>
								<tbody>
									<%
		for (int i=0; i<room_list.size(); i++) {
			RoomItem room_item = room_list.get(i);
%>
									<tr class="text-center row">
										<td class="col-md-3"><%=room_item.getName()%></td>
										<td class="col-md-3"><%=room_item.getLimit_person()%></td>
										<td class="col-md-3"><%=room_item.getOff_price()%></td>
										<td class="col-md-3"><%=room_item.getOn_price()%></td>
									</tr>
									<%		
		}
	}
%>
								</tbody>
							</table>
						</div>
					</div>
				</div>
				
				<div class="form-group">
					<label for="board" class="col-md-2 control-label">민박후기</label>
					<div class="col-md-10">
<%
	if (board_list.size() < 1) {
%>
						<p class="form-control-static">후기 정보가 없습니다.</p>
<%
	} else {
%>
						<div class="table-responsive">
							<table class="table table-striped table-hover">
								<thead>
									<tr class="warning row">
										<th class="text-center col-md-1">번호</th>
										<th class="text-center col-md-2">제목</th>
										<th class="text-center col-md-1">평점</th>
										<th class="text-center col-md-2">작성자</th>
										<th class="text-center col-md-1">조회수</th>
										<th class="text-center col-md-2">등록일자</th>
										<th class="text-center col-md-2">수정일자</th>
										<th class="text-center col-md-1"></th>
									</tr>
								</thead>
								<tbody>
									<%
									if (board_list.size() < 1) {
								%>
									<tr>
										<td colspan="9">
											<p class="text-center text-warning" style="height: 100px">
												저장된 데이터가 없습니다.</p>
										</td>
									</tr>
									<%
									} else {
										for (int i=0; i<board_list.size(); i++) {
											BoardItem board_item = board_list.get(i);
								%>
									<tr class="text-center row">
										<td class="col-md-1"><%=board_item.getSeq()%></td>
										<td class="col-md-2"><a
											href="../board/viewBoard.jsp?seq=<%=board_item.getSeq()%>"><span
												class="link"><%=board_item.getTitle()%><small> <%
											int cnt = db.getCount("comment", "seq", "board_seq = " + board_item.getSeq());
											if (cnt != 0) { out.print(" [" + cnt + "]"); }
										%>
												</small></span></a></td>
										<td class="col-md-1"><%=board_item.getGrade()%>점</td>
										<td class="col-md-2"><a
											href="../member/viewMember.jsp?seq=<%=board_item.getMember_seq()%>"><span
												class="link"><%=board_item.getUser_name()%></span></a></td>
										<td class="col-md-1"><%=board_item.getHit()%></td>
										<td class="col-md-2 small"><%= datetime.getDateTimeString(item.getReg_date()) %></td>
										<td class="col-md-2 small"><%= datetime.getDateTimeString(item.getEdit_date()) %></td>
										<td class="col-md-1"><a
											href="../board/viewBoard.jsp?seq=<%=board_item.getSeq()%>"
											class="btn btn-primary">정보보기</a></td>
									</tr>
									<%
										}
									}
								%>
								</tbody>
							</table>
						</div>
						
		<!-- 페이지 구현 -->
		<div class="text-center">
			<ul class="pagination pagination-centered">
				<%
	// 이전 페이지 그룹 링크
	// --> 현재 페이지가 1페이지의 그룹 수보다 작다면 첫 그룹이므로 링크 비활성화
	if (p_handler.getNowPage() <= p_handler.getPageGroupCount()) {
%>
				<li class="disabled"><a href="#">&laquo;</a></li>
				<%		
	} else {
		// 그렇지 않다면, 이전 페이지 그룹의 마지막 페이지로 링크 생성
		int prevPage = p_handler.getStartPage() -1;
%>
				<li><a
					href="../board/listBoard.jsp?category=<%=category_seq%>&page=<%=prevPage%>">&laquo;</a></li>
				<%
	}
%>




				<%
	// 현재 페이지 그룹의 첫 페이지부터 마지막 페이지까지 반복 처리
	for (int i=p_handler.getStartPage(); i<=p_handler.getEndPage(); i++) {
		String classAttr = "";
		if (i == p_handler.getNowPage()) {
			classAttr = "class='active'";
		}
%>
				<li <%=classAttr%>><a
					href="../board/listBoard.jsp?category=<%=category_seq%>&page=<%=i%>"><%=i%></a></li>
				<%		
	}
%>



				<%
	// 현재 페이지가 첫 마지막 페이지 그룹의 첫 페이지보다 크거나 같다면, 다음 페이지로 이동할 수 없다.
	// --> 현재 페이지가 1페이지의 그룹 수보다 작다면 첫 그룹이므로 링크 비활성화
	if (p_handler.getNowPage() >= p_handler.getLastGroupFirstPage()) {
%>
				<li class="disabled"><a href="#">&raquo;</a></li>
				<%		
	} else {
		// 그렇지 않다면, 현재 그룹의 마지막페이지 + 1 --> 다음 그룹의 첫 페이지로 이동할 링크 생성
		int nextPage = p_handler.getEndPage() + 1;
%>
				<li><a
					href="../board/listBoard.jsp?category=<%=category_seq%>&page=<%=nextPage%>">&raquo;</a></li>
				<%
	}
%>
			</ul>
		</div>
		<!--// 페이지 구현 -->
<%
	}
%>
					</div>
				</div>

				<div class="form-group">
					<div class="col-md-offset-2 col-md-10">
						<div class="row">
							<div class="col-md-4">
								<a href="listHouse.jsp?city=<%=item.getHouse_city_seq()%>"
									class="btn btn-primary btn-block">목록보기</a>
							</div>
							<div class="col-md-4">
								<button type="submit" id="edit" formaction="editHouse.jsp"
									formmethod="post" class="btn btn-info btn-block">수정하기</button>
							</div>
							<div class="col-md-4">
								<button type="submit" id="delete" formaction="deleteHouseOk.jsp"
									formmethod="post" class="btn btn-danger btn-block">삭제하기</button>
							</div>
						</div>
					</div>
				</div>

			</fieldset>
		</form>
	</div>
	<% db.disconnect(); %>
	<!-- Javascript -->
	<script src="/Team-Project/assets/js/jquery.js"></script>
	<script src="/Team-Project/assets/js/bootstrap.min.js"></script>

	<script type="text/javascript">
		$(function() {
			// id 속성이 "delete"인 요소에 대한 click이벤트
			$("#delete").click(function() {
				// 사용자에게 물어본다.
				if (!confirm("정말 삭제하시겠습니까?")) {
					// 취소를 선택한 경우 처리 둥단
					return false;
				}
			});
		});
	</script>

</body>
</html>